<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="音乐名称" prop="musicName">
        <el-input
          v-model="queryParams.musicName"
          placeholder="请输入音乐名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="音乐类型" prop="musicType">
        <el-select v-model="queryParams.musicType" clearable placeholder="请选择音乐类型">
          <el-option
            v-for="item in this.musicTypeList"
            :key="item.musicTypeId"
            :label="item.musicTypeName"
            :value="item.musicTypeId">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="音乐状态" prop="musicStatus">
        <el-select v-model="queryParams.musicStatus" clearable placeholder="请选择音乐状态">
          <el-option
            v-for="item in musicStatus()"
            :key="item.musicStatus"
            :label="item.statusName"
            :value="item.musicStatus">
          </el-option>
        </el-select>
      </el-form-item>
      <!--<el-form-item label="单价" prop="musicPrice">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicPrice"-->
      <!--    placeholder="请输入单价"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="歌词" prop="musicWords">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicWords"-->
      <!--    placeholder="请输入歌词"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="描述" prop="musicDesc">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicDesc"-->
      <!--    placeholder="请输入描述"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="音乐时长" prop="musicTime">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicTime"-->
      <!--    placeholder="请输入音乐时长(单位为秒)"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="收藏数" prop="musicCollect">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicCollect"-->
      <!--    placeholder="请输入收藏数"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="销量" prop="musicSales">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicSales"-->
      <!--    placeholder="请输入销量"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="音频" prop="musicAudio">-->
      <!--  <el-input-->
      <!--    v-model="queryParams.musicAudio"-->
      <!--    placeholder="请选择音频"-->
      <!--    clearable-->
      <!--    @keyup.enter.native="handleQuery"-->
      <!--  />-->
      <!--</el-form-item>-->
      <!--<el-form-item label="发布时间" prop="musicCreateTime">-->
      <!--  <el-date-picker clearable-->
      <!--    v-model="queryParams.musicCreateTime"-->
      <!--    type="date"-->
      <!--    value-format="yyyy-MM-dd"-->
      <!--    placeholder="请选择发布时间">-->
      <!--  </el-date-picker>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['music:music:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['music:music:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['music:music:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['music:music:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="musicList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="音乐名称" align="center" prop="musicName">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.musicName" placement="top">
            <div v-if="scope.row.musicName.length <= 4">{{ scope.row.musicName }}</div>
            <div v-if="scope.row.musicName.length > 4">{{ scope.row.musicName.substring(0, 4) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="歌手" align="center" prop="singerStr">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.singerStr" placement="top">
            <div v-if="scope.row.singerStr.length <= 3">{{ scope.row.singerStr }}</div>
            <div v-if="scope.row.singerStr.length > 3">{{ scope.row.singerStr.substring(0, 3) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="musicType" :formatter="musicTypeFormatter"/>
      <el-table-column label="单价" align="center" prop="musicPrice" :formatter="musicPriceFormatter"/>
      <el-table-column label="歌词" align="center" prop="musicWords">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.musicWords" placement="top">
            <div v-if="scope.row.musicWords.length <= 3">{{ scope.row.musicWords }}</div>
            <div v-if="scope.row.musicWords.length > 3">{{ scope.row.musicWords.substring(0, 3) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="描述" align="center" prop="musicDesc">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.musicDesc" placement="top">
            <div v-if="scope.row.musicDesc.length <= 3">{{ scope.row.musicDesc }}</div>
            <div v-if="scope.row.musicDesc.length > 3">{{ scope.row.musicDesc.substring(0, 3) }}...</div>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="收藏数" align="center" prop="musicCollect"/>
      <el-table-column label="销量" align="center" prop="musicSales"/>
      <el-table-column label="图片" align="center" prop="musicImage" width="60">
        <template slot-scope="scope">
          <image-preview :src="scope.row.musicImage" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="musicStatus" :formatter="musicStatusFormatter"/>
      <el-table-column label="音频" align="center" prop="musicAudio">
        <template slot-scope="scope">
          <div class="play" @click="setMusicUrl(scope.row.musicAudio,scope.row.musicId)">
            <div>
              <svg-icon style="height: 50px" :icon-class="toggle === scope.row.musicId ? 'chart' : 'radio'"/>
            </div>
          </div>
        </template>
      </el-table-column>
      <el-table-column label="发布时间" align="center" prop="musicCreateTime" width="95">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.musicCreateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['music:music:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['music:music:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改音乐列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="音乐名称" prop="musicName">
          <el-input v-model="form.musicName" placeholder="请输入音乐名称"/>
        </el-form-item>
        <el-form-item label="歌手" prop="singerIds">
          <el-select v-model="form.singerIds"
                     clearable
                     multiple
                     collapse-tags
                     placeholder="请选择歌手">
            <el-option
              v-for="item in this.singerListAll"
              :key="item.singerId"
              :label="item.singerName"
              :value="item.singerId">
              <span style="float: left">{{ item.singerName }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{
                  item.singerGender === 1 ? '男' : '女'
                }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="类型" prop="musicType">
          <el-select v-model="form.musicType" clearable placeholder="请选择音乐类型">
            <el-option
              v-for="item in this.musicTypeList"
              :key="item.musicTypeId"
              :label="item.musicTypeName"
              :value="item.musicTypeId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价" prop="musicPrice">
          <el-input v-model="form.musicPrice" placeholder="请输入单价"/>
        </el-form-item>
        <el-form-item label="歌词" prop="musicWords">
          <el-input v-model="form.musicWords" placeholder="请输入歌词"/>
        </el-form-item>
        <el-form-item label="描述" prop="musicDesc">
          <el-input v-model="form.musicDesc" placeholder="请输入描述"/>
        </el-form-item>
        <el-form-item label="时长(秒)" prop="musicTime">
          <!--<el-input v-model="form.musicTime" placeholder="请输入音乐时长(单位为秒)"/>-->
          <el-slider v-model="form.musicTime" :max=300></el-slider>
        </el-form-item>
        <!--<el-form-item label="收藏数" prop="musicCollect">-->
        <!--  <el-input v-model="form.musicCollect" placeholder="请输入收藏数"/>-->
        <!--</el-form-item>-->
        <!--<el-form-item label="销量" prop="musicSales">-->
        <!--  <el-input v-model="form.musicSales" placeholder="请输入销量"/>-->
        <!--</el-form-item>-->
        <el-form-item label="图片" prop="musicImage">
          <image-upload v-model="form.musicImage"/>
        </el-form-item>
        <el-form-item label="状态" prop="musicStatus">
          <el-select v-model="form.musicStatus" clearable placeholder="请选择音乐状态">
            <el-option
              v-for="item in musicStatus()"
              :key="item.musicStatus"
              :label="item.statusName"
              :value="item.musicStatus">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="音频" prop="musicAudio">
          <file-upload v-model="form.musicAudio"/>
        </el-form-item>
        <el-form-item label="发布时间" prop="musicCreateTime" v-if="this.title === '添加音乐列表'">
          <el-date-picker clearable
                          v-model="form.musicCreateTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import {listMusic, getMusic, delMusic, addMusic, updateMusic} from "@/api/music/music";
import {listMusictype} from "@/api/music/musictype";
import musicStatus from "../../../utils/musicStatus";
import {parseTime} from "@/utils/ruoyi";
import {listSinger} from "@/api/music/singer";

export default {
  name: "Music",
  computed: {
    ...mapGetters([
      'isPlay',
      'url'
    ])
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 音乐列表表格数据
      musicList: [],
      // 音乐类型列表表格数据
      musicTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        musicName: null,
        musicType: null,
        musicPrice: null,
        musicWords: null,
        musicDesc: null,
        musicTime: null,
        musicCollect: null,
        musicSales: null,
        musicImage: null,
        musicStatus: null,
        musicAudio: null,
        musicCreateTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      toggle: '',
      singerListAll: []
    };
  },
  created() {
    this.getList();
    this.getMusicTypeList();
    this.getSingerList();
  },
  destroyed() {
    this.$store.commit('setIsPlay', false)
  },
  methods: {
    // 切换播放歌曲
    setMusicUrl(url, id) {
      if (this.url !== url) {
        this.$store.commit('setUrl', url === null ? '' : url)
        this.$store.commit('setIsPlay', true)
        this.toggle = id
        return
      }
      if (this.isPlay) {
        this.toggle = ''
        this.$store.commit('setIsPlay', false)
      } else {
        this.toggle = id
        this.$store.commit('setIsPlay', true)
      }
    },
    parseTime,
    musicStatus() {
      return musicStatus
    },
    /** 音乐状态格式化 */
    musicStatusFormatter(row, column, value, index) {
      for (let i = 0; i < musicStatus.length; i++) {
        if (musicStatus[i].musicStatus === value) {
          return musicStatus[i].statusName
        }
      }
    },
    /** 音乐时长格式化 */
    musicTimeFormatter(seconds) {
      const minutes = Math.floor(seconds / 60);
      const remainingSeconds = seconds % 60;
      return `${minutes < 10 ? '0' + minutes : minutes}:${remainingSeconds < 10 ? '0' + remainingSeconds : remainingSeconds}`;
    },
    /** 音乐单价格式化 */
    musicPriceFormatter(row, column, value, index) {
      return value.toFixed(2);
    },
    /** 音乐类型格式化 */
    musicTypeFormatter(row, column, value, index) {
      for (let i = 0; i < this.musicTypeList.length; i++) {
        if (this.musicTypeList[i].musicTypeId === value) {
          return this.musicTypeList[i].musicTypeName
        }
      }
    },
    /** 查询音乐类型列表列表 */
    getSingerList() {
      this.loading = true;
      listSinger().then(response => {
        this.singerListAll = response.rows;
        this.loading = false;
      });
    },
    /** 查询音乐类型列表列表 */
    getMusicTypeList() {
      this.loading = true;
      listMusictype().then(response => {
        this.musicTypeList = response.rows;
        this.loading = false;
      });
    },
    /** 查询音乐列表列表 */
    getList() {
      this.loading = true;
      listMusic(this.queryParams).then(response => {
        this.musicList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
// 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
// 表单重置
    reset() {
      this.form = {
        musicId: null,
        musicName: null,
        musicType: null,
        musicPrice: null,
        musicWords: null,
        musicDesc: null,
        musicTime: null,
        musicCollect: null,
        musicSales: null,
        musicImage: null,
        musicStatus: null,
        musicAudio: null,
        musicCreateTime: null,
        singerIds: []
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.musicId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加音乐列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const musicId = row.musicId || this.ids
      getMusic(musicId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改音乐列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.musicId != null) {
            updateMusic(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMusic(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const musicIds = row.musicId || this.ids;
      this.$modal.confirm('是否确认删除音乐列表编号为"' + musicIds + '"的数据项？').then(function () {
        return delMusic(musicIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('music/music/export', {
        ...this.queryParams
      }, `music_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.play {
  width: 50px;
  height: 50px;
}
</style>
